<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\DeliveryKey as DeliveryKeyModel;

class DeliveryKey extends Backend
{
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new DeliveryKeyModel();;
    }

    public function add () {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                try {
                    $params['province_id'] = $this->request->post('province');
                    $params['city_id'] = $this->request->post('city');
                    $params['region_id'] = $this->request->post('region');

                    $result = $this->model->allowField(true)->save($params);
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    public function edit ($ids = null) {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $params['province_id'] = $this->request->post('province');
                $params['city_id'] = $this->request->post('city');
                $params['region_id'] = $this->request->post('region');
                $result = false;
                try {
                    $result = $row->allowField(true)->save($params);
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    // /**
    //  * 显示资源列表
    //  *
    //  * @return \think\Response
    //  */
    // public function index()
    // {
    //     if($this->request->isAjax()) {
    //         [$where, $sort, $order, $offset, $limit] = $this->buildparams();
    //         $list = DeliveryKeyModel::where($where)
    //         ->order($sort, $order)
    //         ->paginate($limit);
    //         $result = ['total' => $list->total(), 'rows' => $list->items()];
    //         return json($result);
    //     }
    //     return $this->fetch();
    // }
}
